//
//  ZMInputImgCVCell.m
//  FM_Project
//
//  Created by chenzm on 2019/8/9.
//  Copyright © 2019 DL. All rights reserved.
//

#import "ZMInputImgCVCell.h"

@interface ZMInputImgCVCell()


@end

@implementation ZMInputImgCVCell{
    UIImage *img;
}

- (void)setModel:(BaseM *)model {

    UIImage *defImg = kImage(model.bsDefImgStr1);
    if (model.bsImgUrl1) {
        if ([model.bsImgUrl1 isKindOfClass:[NSString class]]) {
            if ([model.bsImgUrl1 hasPrefix:@"http"]) {
                Weakself(self);
                [self.imgV sd_setImageWithURL:kURL(model.bsImgUrl1) placeholderImage:defImg completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                    if (weakself.imageFreshBlock) {
                        if (!image) {
                            weakself.imageFreshBlock(defImg);
                        } else {
                            weakself.imageFreshBlock(image);
                        }
                    }
                }];
            }else{
                self.imgV.image = kIsEmptyStr(model.bsImgUrl1)?kImage(model.bsDefImgStr1):kImage(model.bsImgUrl1);
            }
        }else if([model.bsImgUrl1 isKindOfClass:[UIImage class]]){
            if ([model.bsImgUrl1 isKindOfClass:NSClassFromString(@"_UIAnimatedImage")]) {
                return;
            }
            self.imgV.image = (UIImage *)model.bsDefImgStr1;
        }
    } else {
        if (model.bsImage) {
            self.imgV.image = model.bsImage;
        }else{
            self.imgV.image = defImg;
        }
    }
    
    if (model.bsCornerRaf > 0) {
        [ZMSpeedy zm_changeControl:self.imgV cornerRadius:model.bsCornerRaf borderWidth:0 borderColor:nil mask:YES];
    }
    
    self.deleteBtn.hidden = !model.bsIsShow1;

}

-(void)btnAction:(UIButton *)sender{
    !self.deleteBtnBlock?:self.deleteBtnBlock();
}

#pragma mark - Intial
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = kClearColor;
        [self setBaseData];
        [self setUpUI];
    }
    return self;
}

-(void)setBaseData{
    img = kImage(@"icon_deleteClose_black");
}

- (void)setUpUI{
    [self imgV];
    [self deleteBtn];
}

#pragma mark - 布局
- (void)layoutSubviews{
    [super layoutSubviews];
}

#pragma mark - lazyLoad
-(UIImageView *)imgV{
    if (!_imgV) {
        _imgV = [[UIImageView alloc] init];
        _imgV.contentMode = UIViewContentModeScaleAspectFill;
        _imgV.clipsToBounds = YES;
        [self addSubview:_imgV];
        [_imgV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self).offset(5);
            make.left.mas_equalTo(self).offset(5);
            make.bottom.mas_equalTo(self).offset(-5);
            make.right.mas_equalTo(self).offset(-5);
        }];
    }
    return _imgV;
}

-(UIButton *)deleteBtn{
    if (!_deleteBtn) {
        _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _deleteBtn.titleLabel.font = kFont(13);
        [_deleteBtn setImage:img forState:UIControlStateNormal];
//        [_deleteBtn setBackgroundImage:img forState:UIControlStateNormal];
        [_deleteBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_deleteBtn];
        BlockSelf(img);
        [_deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self);
            make.right.mas_equalTo(self);
//            make.size.mas_equalTo(blockimg.size);
            make.width.height.mas_equalTo(25);
        }];
    }
    return _deleteBtn;
}

@end
